package mirkofrancuski.battleship;

import mirkofrancuski.battleship.db.DatabaseHelper;
import mirkofrancuski.battleship.game.Board;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends MenuActivity {
	private static final String DEBUG_TAG = "DEBUG MainActivity";
	private static final String GAME_STARTED = "game_started";

	// @SuppressWarnings("deprecation")
	public static final int DIALOG_EXIT_ID = 0;
	public static final int REQUEST_BOARD = 1;
	public static final int REQUEST_NEW_GAME = 2;

	private Boolean gameStarted = false;

	private DatabaseHelper m_dbHelper;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
		setContentView(R.layout.activity_main);

		m_dbHelper = new DatabaseHelper(this);

		SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);
		gameStarted = settingsActivity.getBoolean(GAME_STARTED, false);

		// set font
		Button txt = (Button) findViewById(R.id.button_new_game);
		Typeface font = Typeface.createFromAsset(getAssets(), "ArmyChalk.ttf");
		txt.setTypeface(font);

		txt = (Button) findViewById(R.id.button_high_scores);
		txt.setTypeface(font);

		txt = (Button) findViewById(R.id.button_settings);
		txt.setTypeface(font);

	}

	@Override
	protected void onDestroy() {
		Log.d(DEBUG_TAG, "onDestory");
		super.onDestroy();

		SharedPreferences gameSettings = getPreferences(MODE_PRIVATE);
		SharedPreferences.Editor prefEditor = gameSettings.edit();
		prefEditor.putBoolean("GAME_STARTED", gameStarted);
		prefEditor.commit();
	}

	@Override
	protected void onPause() {
		Log.d(DEBUG_TAG, "onPause");
		super.onPause();

		if (isFinishing()) {
			// stop music
		}
	}

	@Override
	protected void onResume() {
		Log.d(DEBUG_TAG, "onResume");
		super.onResume();

		// int color = getResources().getColor(R.color.Black);
		//
		// TextView txt = (TextView) findViewById(R.id.button_new_game);
		// txt.setTextColor(color);
		// txt.setBackgroundResource(R.drawable.button);
		//
		// txt = (TextView) findViewById(R.id.button_high_scores);
		// txt.setTextColor(color);
		// txt.setBackgroundResource(R.drawable.button);

	}

	public void launchSettings(View view) {
		startActivity(new Intent(this, SettingsActivity.class));
	};

	public void newGame(View view) {
		// int color = getResources().getColor(R.color.White);
		// TextView txt = (TextView) findViewById(R.id.button_new_game);
		// txt.setTextColor(color);
		// txt.setBackgroundResource(R.drawable.button_pressed);

		if (gameStarted) {
			Intent intent = new Intent(this, GameActivity.class);
			startActivityForResult(intent, REQUEST_NEW_GAME);
		} else {
			Intent intent = new Intent(this, PlacementActivity.class);
			startActivityForResult(intent, REQUEST_BOARD);
		}

	}

	public void startGame(Board board) {
		new Intent(this, GameActivity.class);
	}

	public void highscore(View view) {
		// int color = getResources().getColor(R.color.White);
		// TextView txt = (TextView) findViewById(R.id.button_high_scores);
		// txt.setTextColor(color);
		// txt.setBackgroundResource(R.drawable.button_pressed);

		startActivity(new Intent(this, HighScoreActivity.class));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_BOARD)
			if (resultCode == RESULT_OK) {
				Board board = data.getParcelableExtra("resultBoard");

				Intent intent = new Intent(this, GameActivity.class);
				intent.putExtra("secundaryBoard", board);
				startActivityForResult(intent, REQUEST_NEW_GAME);
				gameStarted = true;
			}
		if (requestCode == REQUEST_NEW_GAME) {			
			switch (resultCode) {
			case RESULT_OK:
				newGame(null);
				break;
			case RESULT_FIRST_USER:
				highscore(null);
				gameStarted = false;
				break;
			default:
				break;
			}
		}

		requestCode = 0;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
					&& event.getRepeatCount() == 0) {
				onBackPressed();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	};

	@SuppressWarnings("deprecation")
	public void onBackPressed() {
		showDialog(DIALOG_EXIT_ID);
	}

	@SuppressWarnings("deprecation")
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
		case DIALOG_EXIT_ID:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(android.R.drawable.ic_dialog_alert)
					.setTitle(R.string.exit_dialog_title)
					.setCancelable(false)
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									MainActivity.this.finish();
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			dialog = builder.create();
			break;

		default:
			dialog = super.onCreateDialog(id);
			break;
		}

		return dialog;
	}

}
